package main

import(
    "context"
    "fmt"
)

func main(){
    ctx, cancel := context.WithCancel(context.Background())
    v := make(chan int)
    go worker(ctx, v)
    for{
        i := <-v
        fmt.Println(i)
        if i == 5{
            cancel()
            return
        }
    }
}

func worker(c context.Context, v chan int){
    l := 0
    for{
        select{
            case <-c.Done():
                return
            default:
                l++
                v <- l
        }
    }
}